In [1]:
#the usual beginning
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from pandas import Series, DataFrame, concat
#define any string with 'C' as NaN. 'C' is an indicator for the Masimo as a low signal reading
def readD(val):
if 'C' in val:
return np.nan
return val
In [2]:
# MAKE SURE YOU CHANGE THIS!!!
#
#
#
BabyNumber = 'ROP018'
In [3]:
dfNIRSpre0 = pd.read_csv('/Users/John/Dropbox/LLU/ROP/NIRS/Clean/' + BabyNumber + 'NIRS.csv',
parse_dates={'timestamp': ['Date',' Time']},
index_col='timestamp',
usecols=['Date', ' Time', ' Ch2 %StO2'],
na_values=['0'])
dfPOpre = pd.read_csv('/Users/John/Dropbox/LLU/ROP/Pulse Ox/' + BabyNumber + 'PO.csv',
parse_dates={'timestamp': ['Date','Time']},
index_col='timestamp',
usecols=['Date', 'Time', 'SpO2', 'PR', 'PI', 'Exceptions'],
na_values=['0'],
converters={'Exceptions': readD}
)
dfNIRSpre = dfNIRSpre0.rename(columns={' Ch2 %StO2': 'StO2'}) #rename NIRS column
#parse_dates tells the read_csv function to combine the date and time column
#into one timestamp column and parse it as a timestamp.
# pandas is smart enough to know how to parse a date in various formats
#index_col sets the timestamp column to be the index.
#usecols tells the read_csv function to select only the subset of the columns.
#na_values is used to turn 0 into NaN
#converters: readD is the dict that means any string with 'C' with be NaN (for PI)
In [4]:
#phone almost always equals Philips monitor
"""NIRS date/time is 2 mins and 10 seconds slower than phone. Have to correct for it."""
TCnirs = timedelta(minutes=2, seconds=10)
"""Pulse ox date/time is 1 mins and 32 seconds faster than phone. Have to correct for it."""
TCpo = timedelta(minutes=1, seconds=32)
In [5]:
#time correct NIRS dataframe
dfNIRS = dfNIRSpre.set_index([dfNIRSpre.index-TCnirs])
#df.set_index(['STK_ID','RPT_Date'], inplace = True)
#inplace has to be false because otherwise it won't create a new dataframe and it'll return "None" instead
#time correct PO dataframe
dfPO = dfPOpre.set_index([dfPOpre.index+TCpo])
In [6]:
#Clean the dataframe to get rid of rows that have NaN for PI purposes
dfPO_clean = dfPO[dfPO.loc[:, ['PI', 'Exceptions']].apply(pd.notnull).all(1)]
In [7]:
#Combine two DataFrame objects and default to non-null values in frame calling the method.
#Result index columns will be the union of the respective indexes and columns
df = dfNIRS.combine_first(dfPO_clean)
In [8]:
df_first = df.first_valid_index() #get the first number from index
Y = pd.to_datetime(df_first) #convert index to datetime
# Y = TIME DATA COLLECTION BEGAN / First data point on CSV
# SYNTAX:
# datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
W1 = datetime(2016, 1, 20, 7, 35)
# W = first eye drop start
X = datetime(2016, 1, 20, 8, 42)
# X = ROP Exam Started
Z = datetime(2016, 1, 20, 8, 46)
# Z = ROP Exam Ended
df_last = df.last_valid_index() #get the last number from index
Q = pd.to_datetime(df_last)
# Q = TIME DATA COLLECTION ENDED / Last Data point on CSV
In [9]:
W2 = datetime(2016, 1, 20, 7, 35)
#W2 = second eyedrops
W3 = datetime(2016, 1, 20, 7, 40)
#W3 = third eyedrops
In [10]:
avg0NIRS = df.StO2[Y:W1].mean()
avg0PI = df.PI[Y:W1].mean()
avg0O2 = df.SpO2[Y:W1].mean()
avg0PR = df.PR[Y:W1].mean()
print 'Baseline Averages\n', 'NIRS :\t', avg0NIRS, '\nPI :\t',avg0PI, '\nSpO2 :\t',avg0O2,'\nPR :\t',avg0PR,
#df.std() for standard deviation
In [11]:
def perdeltadrop1(start, end, delta):
rdrop1 = []
curr = start
while curr < end:
rdrop1.append(curr)
curr += delta
return rdrop1
dfdrop1NI = df.StO2[W1:W1+timedelta(minutes=5)]
dfdrop1PI = df.PI[W1:W1+timedelta(minutes=5)]
dfdrop1O2 = df.SpO2[W1:W1+timedelta(minutes=5)]
dfdrop1PR = df.PR[W1:W1+timedelta(minutes=5)]
windrop1 = timedelta(seconds=10)
rdrop1 = perdeltadrop1(W1, W1+timedelta(minutes=5), windrop1)
avgdrop1NI = Series(index = rdrop1, name = 'StO2 1st ED')
avgdrop1PI = Series(index = rdrop1, name = 'PI 1st ED')
avgdrop1O2 = Series(index = rdrop1, name = 'SpO2 1st ED')
avgdrop1PR = Series(index = rdrop1, name = 'PR 1st ED')
for i in rdrop1:
avgdrop1NI[i] = dfdrop1NI[i:(i+windrop1)].mean()
avgdrop1PI[i] = dfdrop1PI[i:(i+windrop1)].mean()
avgdrop1O2[i] = dfdrop1O2[i:(i+windrop1)].mean()
avgdrop1PR[i] = dfdrop1PR[i:(i+windrop1)].mean()
resultdrops1 = concat([avgdrop1NI, avgdrop1PI, avgdrop1O2, avgdrop1PR], axis=1, join='inner')
print resultdrops1
In [12]:
import matplotlib.pyplot as plt
#have graphs in ipython notebook
%matplotlib inline
In [13]:
dfgraph = df.drop(['Exceptions'], 1) #remove Exceptions
In [17]:
plt.figure() #http://matplotlib.org/api/figure_api.html customize the image
dfgraph.plot(subplots=True, figsize=(6, 6), use_index=False)
x=0
#plt.title('NIRS')
#plt.xlabel('%StO2')
#plt.ylabel('Time')
#plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.show()
In [ ]: